package com.lm.controller;

import com.lm.model.DpChannelData;
import com.lm.service.DpAreaLimitService;
import com.lm.service.DpChannelDataService;
import com.lm.service.DpChannelService;
import com.meirengu.common.StatusCode;
import com.meirengu.controller.BaseController;
import com.meirengu.model.Result;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("dpChannelData")
public class DpChannelDataController extends BaseController {


    private static final Logger logger = LoggerFactory.getLogger(DpChannelDataController.class);

    @Autowired
    private DpChannelService dpChannelService;

    @Autowired
    private DpChannelDataService dpChannelDataService;

    @Autowired
    private DpAreaLimitService dpAreaLimitService;


    /**
     * 定时任务生成第二天渠道数据
     *
     * @return
     */
    @RequestMapping(value = "addByJob", method = RequestMethod.GET)
    public Result addByJob() {
        try {
            /*1.查询出未下架的渠道
            2.根据渠道查询出该渠道的地区限制
            3.根据地区限制生成渠道第二天数据*/
            Map<String, Object> dpChannelParam = new HashMap<>();
            /*后续添加是否下架字段条件*/
            List<Map<String, Object>> dpChannelList = dpChannelService.getList(dpChannelParam);
            if (dpChannelList != null && dpChannelList.size() > 0) {
                for (Map<String, Object> dpChannelMap : dpChannelList) {
                    String channelNo = dpChannelMap.get("channelNo").toString();
                    String channelName = dpChannelMap.get("channelName").toString();
                    String staffAccount = dpChannelMap.get("staffAccount").toString();
                    Map<String, Object> dpAreaLimitParam = new HashMap<>();
                    dpAreaLimitParam.put("channelNo", channelNo);
                    List<Map<String, Object>> areaLimitlist = dpAreaLimitService.getList(dpAreaLimitParam);
                    if (areaLimitlist != null && areaLimitlist.size() > 0) {
                        for (Map<String, Object> areaLimitMap : areaLimitlist) {
                            String areaCode = areaLimitMap.get("areaCode").toString();
                            String areaName = areaLimitMap.get("areaName").toString();
                            Integer num = (int) areaLimitMap.get("num");
                            DpChannelData dpChannelData = new DpChannelData();
                            dpChannelData.setCreateName(staffAccount);
                            dpChannelData.setChannelNo(channelNo);
                            dpChannelData.setChannelName(channelName);
                            dpChannelData.setAreaCode(areaCode);
                            dpChannelData.setAreaName(areaName);
                            dpChannelData.setNum(num);
                            dpChannelData.setDataDate(DateUtils.addDays(new Date(), 1));
                            dpChannelDataService.insert(dpChannelData);
                        }
                    }
                }
            }
            return super.setResult(StatusCode.OK, "", StatusCode.codeMsgMap.get(StatusCode.OK));
        } catch (Exception e) {
            e.printStackTrace();
            String params = "";
            logger.error("DpChannelDataController addByJob throws Exceprion :{} happens. params:{}.", ExceptionUtils.getFullStackTrace(e), params);
            return super.setResult(StatusCode.INTERNAL_SERVER_ERROR, "", StatusCode.codeMsgMap.get(StatusCode.INTERNAL_SERVER_ERROR));
        }
    }


}
